package model

import (
	"gorm.io/gorm/clause"
	"server/common"
)

type LinuxCommand struct {
	Model
	Name     string `json:"name" gorm:"column:name;NOT NULL;type:varchar(16);"`
	Command  string `json:"command" gorm:"column:command;NOT NULL;type:longtext;"`
	Desc     string `json:"desc" gorm:"column:desc;type:longtext;"`
	GateGory string `json:"gategory"`

	OwnerID int `json:"owner_id"`
	Owner   Owner

	ProjectID int `json:"project_id"`
	Project   Project
}

func (command *LinuxCommand) Create() error {
	if err := common.DB.Create(&command).Error; err != nil {
		return err
	}
	return nil
}

func (command *LinuxCommand) Info() error {
	if err := common.DB.Where("id = ?", command.ID).Preload(clause.Associations).First(&command).Error; err != nil {
		return err
	}
	return nil
}

func (command *LinuxCommand) Update() error {
	if err := common.DB.Where("id = ?", command.ID).Updates(&command).Error; err != nil {
		return err
	}
	return nil
}

func (command *LinuxCommand) Delete() error {
	if err := common.DB.Where("id = ?", command.ID).Delete(&command).Error; err != nil {
		return err
	}
	return nil
}
